dangqian<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { authApi } from '../../services/api'

// 表单数据
const registerForm = ref({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

const router = useRouter()
const loading = ref(false)
const errorMessage = ref<string | null>(null)

// 注册方法
const register = async () => {
  if (!registerForm.value.username.trim()) {
    window.showNotification('请输入用户名', 'error')
    return
  }
  
  if (!registerForm.value.email.trim()) {
    window.showNotification('请输入邮箱', 'error')
    return
  }
  
  if (!registerForm.value.password) {
    window.showNotification('请输入密码', 'error')
    return
  }
  
  if (registerForm.value.password !== registerForm.value.confirmPassword) {
    window.showNotification('两次输入的密码不一致', 'error')
    return
  }
  
  try {
    loading.value = true
    errorMessage.value = null
    
    await authApi.register({
      username: registerForm.value.username,
      email: registerForm.value.email,
      password: registerForm.value.password
    })
    
    window.showNotification('注册成功！', 'success')
    router.push('/login')
  } catch (err) {
    console.error('注册失败:', err)
    let errorMsg = '注册失败'
    
    if (err instanceof Error) {
      // 用户名已存在
      if (err.message.includes('409')) {
        errorMsg = '用户名已存在'
      }
      // 网络错误
      else if (err.message.includes('Failed to fetch') || err.message.includes('fetch')) {
        errorMsg = '无法连接到服务器，请确保后端服务正在运行'
      } else {
        errorMsg = err.message
      }
    } else if (typeof err === 'string') {
      errorMsg = err
    }
    
    errorMessage.value = errorMsg
    window.showNotification(errorMsg, 'error')
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<template>
  <div class="register-container">


    <div class="register-form neo-brutalist">
      <h1>用户注册</h1>
      
      <!-- 错误信息 -->
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
      
      <form @submit.prevent="register">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            id="username"
            v-model="registerForm.username"
            type="text"
            placeholder="输入用户名"
            :disabled="loading"
            class="neo-brutalist-input"
            maxlength="20"
          />
        </div>
        
        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            id="email"
            v-model="registerForm.email"
            type="email"
            placeholder="输入邮箱"
            :disabled="loading"
            class="neo-brutalist-input"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            id="password"
            v-model="registerForm.password"
            type="password"
            placeholder="输入密码"
            :disabled="loading"
            class="neo-brutalist-input"
            minlength="6"
          />
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            id="confirmPassword"
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="再次输入密码"
            :disabled="loading"
            class="neo-brutalist-input"
            minlength="6"
          />
        </div>
        
        <button
          type="submit"
          class="register-button neo-brutalist btn btn--primary"
          :disabled="loading"
          :class="{ 'btn--disabled': loading }"
        >
          {{ loading ? '注册中...' : '注册' }}
        </button>
      </form>
      
      <div class="form-footer">
        <p>已有账户？</p>
        <button
          class="login-button neo-brutalist btn btn--secondary"
          @click="goToLogin"
          :disabled="loading"
        >
          去登录
        </button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 80vh;
  padding: 0 1rem;
  position: relative;
}

/* 导航栏样式 */
.navbar {
  position: absolute;
  top: 1rem;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 800px;
  padding: 1rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 10;
}

.navbar-brand {
  font-size: 1.5rem;
  font-weight: bold;
  font-family: 'Bebas Neue', sans-serif;
  letter-spacing: 2px;
  color: #000;
}

.navbar-right {
  display: flex;
  align-items: center;
}

.register-form {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background: #fff;
  z-index: 1;
  position: relative;
}

.register-form h1 {
  text-align: center;
  margin-bottom: 2rem;
  color: #000;
  font-family: 'Bebas Neue', sans-serif;
  letter-spacing: 2px;
  font-size: 2.5rem;
  text-transform: uppercase;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #000;
  font-weight: 500;
  font-family: 'Bebas Neue', sans-serif;
  letter-spacing: 1px;
  font-size: 1.1rem;
  width: 100%;
}

.neo-brutalist-input {
  width: 100%;
  padding: 0.75rem;
  font-size: 1rem;
  border: 4px solid #000;
  box-shadow: 4px 4px 0 #000;
  font-family: 'Roboto Mono', monospace;
  background-color: #fff;
  box-sizing: border-box;
}

.neo-brutalist-input:focus {
  outline: none;
  box-shadow: 2px 2px 0 #000;
  transform: translate(2px, 2px);
}

.neo-brutalist-input:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.form-footer {
  text-align: center;
  margin-top: 1.5rem;
}

.form-footer p {
  margin-bottom: 1rem;
  color: #000;
  font-family: 'Roboto Mono', monospace;
}

.form-footer .login-button {
  width: 100%;
  padding: 0.75rem;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1.1rem;
  letter-spacing: 1px;
  font-weight: bold;
  text-transform: uppercase;
}

/* Neo Brutalist Styles */
.neo-brutalist {
  border: 4px solid #000;
  box-shadow: 8px 8px 0 #000;
  transition: all 0.2s ease;
  border-radius: 4px;
  background-color: #fff;
}

.neo-brutalist:hover {
  box-shadow: 6px 6px 0 #000;
  transform: translate(2px, 2px);
}

.neo-brutalist.btn {
  font-weight: bold;
  padding: 0.75rem 1.5rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  cursor: pointer;
  transition: none;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1.1rem;
  border: 4px solid #000;
  box-shadow: 4px 4px 0 #000;
  width: 100%;
  box-sizing: border-box;
}

.neo-brutalist.btn:hover {
  background-color: var(--neo-brutalist-yellow);
  color: #000;
  box-shadow: 2px 2px 0 #000;
  transform: translate(2px, 2px);
}

.neo-brutalist.btn--primary {
  background-color: var(--neo-brutalist-yellow);
  color: #000;
}

.neo-brutalist.btn--secondary {
  background-color: #fff;
  color: #000;
  border: 4px solid #000;
  box-shadow: 4px 4px 0 #000;
}

.neo-brutalist.btn.btn--disabled {
  background-color: #ccc;
  cursor: not-allowed;
  box-shadow: 4px 4px 0 #999;
}

.neo-brutalist.btn.btn--disabled:hover {
  box-shadow: 4px 4px 0 #999;
  transform: none;
}

/* 错误消息样式 */
.error-message {
  padding: 0.75rem;
  background-color: var(--neo-brutalist-red);
  color: #fff;
  margin-bottom: 1rem;
  text-align: center;
  font-weight: bold;
  border: 4px solid #000;
  box-shadow: 4px 4px 0 #000;
  font-family: 'Roboto Mono', monospace;
}
</style>